package ui;

import java.awt.Font;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.UnicodeFont;
import org.newdawn.slick.font.effects.ColorEffect;
import org.newdawn.slick.gui.TextField;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.state.transition.FadeInTransition;
import org.newdawn.slick.state.transition.FadeOutTransition;

public class GameMenuState extends BaseMenuState {
	private boolean hasActiveGame = false;
	private boolean showLevelDialog = false;
	private TextField levelInput;

	public GameMenuState() {
		super();
	}
	
	@Override
	public void init(GameContainer gc, StateBasedGame state) throws SlickException {
		// draw menu buttons - new game, options, exit
		addButton(gc, "new_game", GameStateSwitcher.GAME_STATE);
		addButton(gc, "options", GameStateSwitcher.OPTION_STATE);
		addButton(gc, "exit", -1);
		UnicodeFont menuFont;
		try {
			menuFont = new UnicodeFont("resources/menu/HARLOWSI.TTF", 20, false, false);
			menuFont.getEffects().add(new ColorEffect(java.awt.Color.white));
			menuFont.addAsciiGlyphs();
			menuFont.loadGlyphs();
			gc.setDefaultFont(menuFont);
		} catch (SlickException e) {
			menuFont = null;
			e.printStackTrace();
		}
	}

	@Override
	public void render(GameContainer gc, StateBasedGame state, Graphics g) throws SlickException {
		// write title
		g.drawImage(new Image("resources/menu/title.png"),
				gc.getWidth() / 2 - titleWidth / 2, componentSpacing);
		if (showLevelDialog) {
			levelInput = new TextField(gc, new UnicodeFont(Font.decode("Verdana")), gc.getWidth() / 2 - titleWidth / 2, componentSpacing + 100, 200, 200);
			levelInput.render(gc, g);
			levelInput.setFocus(true);
		}
		for (ButtonComponent button : menuItems){
			button.render(gc, g);
		}
	}

	@Override
	public void update(GameContainer gc, StateBasedGame state, int delta) throws SlickException {
		// check if a button is clicked
		for (ButtonComponent button : menuItems) {
			button.update(gc);
			if (button.isClicked()){
				int stateID = button.getStateID();
				String buttonText = button.getText();
				if (stateID != -1) {
					// load the level if it hasn't been loaded yet
					String newLevelName = "level1";
					if (buttonText.equals("new_game")){
						((GameStateSwitcher)state).loadLevel(newLevelName, !hasActiveGame); // TODO let user load own level
						button.setText("resume");
						addButtonAtIndex(gc, 1,  "Restart level", GameStateSwitcher.GAME_STATE);
						hasActiveGame = true;
					}
					if (buttonText.equals("Restart level")) {
						newLevelName = "level" + ((GameStateSwitcher)state).getCurrentLevel();
						((GameStateSwitcher)state).loadLevel(newLevelName, true);
					}
					state.enterState(stateID, new FadeOutTransition(), new FadeInTransition());
				} else {
					if (buttonText.equals("exit")){
						gc.exit();
					}
				}
				break;
			}
		}
	}
	
	@Override
	public int getID() {
		return GameStateSwitcher.MENU_STATE;
	}
	
	public void resetGameMenuState(GameContainer gc) {
		menuItems.clear();
		addButton(gc, "new_game", GameStateSwitcher.GAME_STATE);
		addButton(gc, "options", GameStateSwitcher.OPTION_STATE);
		addButton(gc, "exit", -1);
		hasActiveGame = false;
	}
	
	private void addButton(GameContainer gc, String buttonText, int stateID) {
		ButtonComponent newButton = new ButtonComponent(gc, buttonText, menuItems.size(), stateID);
		menuItems.add(newButton);
	}
	
	private void addButtonAtIndex(GameContainer gc, int index, String buttonText, int stateID) {
		ButtonComponent newButton = new ButtonComponent(gc, buttonText, menuItems.size(), stateID);
		menuItems.add(index, newButton);
	}
}
